package com.hhq.sso.system.controller;

import static org.springblade.core.tool.utils.CacheUtil.SYS_CACHE;

import java.util.List;
import java.util.Map;

import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.CacheUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhq.sso.auth.base.pojo.SystemUserInfo;
import com.hhq.sso.auth.base.util.SystemUserContext;
import com.hhq.sso.common.api.CommonResult;
import com.hhq.sso.common.page.Condition;
import com.hhq.sso.system.entity.Role;
import com.hhq.sso.system.pojo.Grant;
import com.hhq.sso.system.service.IRoleService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;

/**
 * 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Tag(name = "角色", description = "角色")
public class RoleController {

    private IRoleService roleService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @Operation(summary = "详情", description = "传入role")
    public CommonResult<Role> detail(Role role) {
        Role detail = roleService.getOne(Condition.getQueryWrapper(role));
        return CommonResult.data(detail);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @Parameters({
        @Parameter(name = "roleName", description = "参数名称", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "roleAlias", description = "角色别名", in = ParameterIn.QUERY,
            schema = @Schema(type = "string"))})
    @Operation(summary = "列表", description = "传入role")
    public CommonResult<List<Role>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> role) {
        SystemUserInfo user = SystemUserContext.getSystemUserInfo();
        QueryWrapper<Role> queryWrapper = Condition.getQueryWrapper(role, Role.class);
        List<Role> list = roleService.list((!user.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID))
            ? queryWrapper.lambda().eq(Role::getTenantId, user.getTenantId()) : queryWrapper);
        return CommonResult.data(list);
    }

    /**
     * 获取角色树形结构
     */
    @GetMapping("/tree")
    @Operation(summary = "树形结构", description = "树形结构")
    public CommonResult<List<Role>> tree(String tenantId) {
        SystemUserInfo user = SystemUserContext.getSystemUserInfo();
        List<Role> tree = roleService.tree(Func.toStr(tenantId, user.getTenantId()));
        return CommonResult.data(tree);
    }

    /**
     * 获取指定角色树形结构
     */
    @GetMapping("/tree-by-id")
    @Operation(summary = "树形结构", description = "树形结构")
    public CommonResult<List<Role>> treeById(Long roleId) {
        SystemUserInfo user = SystemUserContext.getSystemUserInfo();
        Role role = roleService.getById(roleId);
        List<Role> tree = roleService.tree(Func.notNull(role) ? role.getTenantId() : user.getTenantId());
        return CommonResult.data(tree);
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @Operation(summary = "新增或修改", description = "传入role")
    public CommonResult<Boolean> submit(@Valid @RequestBody Role role) {
        SystemUserInfo user = SystemUserContext.getSystemUserInfo();
        CacheUtil.clear(SYS_CACHE);
        if (Func.isEmpty(role.getId())) {
            role.setTenantId(user.getTenantId());
        }
        return CommonResult.status(roleService.saveOrUpdate(role));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @Operation(summary = "删除", description = "传入ids")
    public CommonResult<Boolean> remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
        return CommonResult.status(roleService.removeByIds(Func.toLongList(ids)));
    }

    /**
     * 设置菜单权限
     */
    @PostMapping("/grant")
    @Operation(summary = "权限设置", description = "传入roleId集合以及menuId集合")
    public CommonResult<Boolean> grant(@RequestBody Grant grant) {
        boolean temp = roleService.grant(grant.getRoleIds(), grant.getMenuIds(), grant.getDataScopeIds());
        return CommonResult.status(temp);
    }
}
